#ifndef _BQ769XX1_H
#define _BQ769XX1_H
#include "sys_hc32f460.h"

/*******************************************************************************
 * register map
 ******************************************************************************/
#define SYS_STAT                0x00
#define CELLBAL1                0x01
#define CELLBAL2                0x02
#define CELLBAL3                0x03
#define SYS_CTRL1               0x04
#define SYS_CTRL2               0x05
#define PROTECT1                0x06
#define PROTECT2                0x07
#define PROTECT3                0x08
#define OV_TRIP                 0x09
#define UV_TRIP                 0x0A
#define CC_CFG                  0x0B

#define TS1_HI_BYTE             0x2C
#define TS1_LO_BYTE             0x2D
#define TS2_HI_BYTE             0x2E
#define TS2_LO_BYTE             0x2F
#define TS3_HI_BYTE             0x30
#define TS3_LO_BYTE             0x31

#define CC_HI_BYTE              0x32
#define CC_LO_BYTE              0x33

#define ADCGAIN1                0x50
#define ADCOFFSET               0x51
#define ADCGAIN2                0x59

    
/*******************************************************************************
 * for bit clear operations of the SYS_STAT register
 ******************************************************************************/
#define STAT_CC_READY           (0x80)
#define STAT_DEVICE_XREADY      (0x20)
#define STAT_OVRD_ALERT         (0x10)
#define STAT_UV                 (0x08)
#define STAT_OV                 (0x04)
#define STAT_SCD                (0x02)
#define STAT_OCD                (0x01)
#define STAT_FLAGS              (0x3F)
//bq76940_930
//BQ的组合方式
typedef enum{
	bq76920_only = 0x01,
	bq76930_only = 0x02,
	bq76940_only = 0x03,
	bq76930_two  = 0x04,
	bq76930_940  = 0x05,
}BQ_MAKEUP_MODE;

typedef union bq796x0_SYS_REG_STAT {
    struct {
        uint8_t OCD            :1;
        uint8_t SCD            :1;
        uint8_t OV             :1;
        uint8_t UV             :1;
        uint8_t OVRD_ALERT     :1;
        uint8_t DEVICE_XREADY  :1;
        uint8_t WAKE           :1;
        uint8_t CC_READY       :1;
    } bits;
    uint8_t regByte;
}BQ_SYS_STAT;

typedef union bq796x0_REG_STAT {
    struct {
		uint8_t CHG_MOS 		:1;
		uint8_t DSG_MOS    		:1;
		uint8_t PRE_MOS			:1;
		uint8_t CHG_LI 			:1;
		uint8_t RM				:4;
    } bits;
    uint8_t regByte;
}BQ_CTRL2_STAT;

typedef struct{
	BQ_SYS_STAT sys_reg;
	BQ_CTRL2_STAT ctrl2_reg;
}BQ_SYS_CTRL_REG_STATUS;

typedef enum{
	BQ_DOWN = 0x00,
	BQ_UP   = 0x01,
}BQ_Side;

typedef struct{
	int bq_gain[2];
	int bq_offset[2];
	uint8_t bq_cur_offset;
}Bq769x0CaliInfo;

uint8_t get_bq_makeup_mode(void);

void bq769x0_reboot(void);
void bq769x0_init(void);
void bq769x0_reboot_init(void);
uint8_t   bq769x0_chg_ctrl(uint8_t ctrlCode);
uint8_t   bq769x0_dsg_ctrl(uint8_t ctrlCode);
uint8_t   pre_charge_ctrl(uint8_t ctrlCode);
uint8_t   charge_li_ctrl(uint8_t ctrlCode);

bool clear_BQbalance_state(void);
void bq769x0_balance_ctrl(BQ_Side bqSide,uint32_t balCode);
void bq769x0_set_SCD(uint16_t curVol,uint16_t delay);
void bq769x0_set_OCD(uint16_t curVol,uint16_t delay);
void bq769x0_set_OV_UV_Delay(uint8_t odelay,uint8_t udelay);
void bq769x0_set_OV_UV(uint16_t overVol,uint16_t underVol);
uint8_t   bq769x0_read_vol(BQ_Side bqSide,uint16_t *volArray);
void destory_i2c(void);
void bq769x0_sleep_enter(void);
void bq769x0_sleep_exit(void);
void bq769x0_update_status(void);
void bq769x0_read_rsense_vol(int *cur_mA);
BQ_SYS_CTRL_REG_STATUS get_bq769x0_status(void);
void bq_irq_timeout_deal(void);
uint8_t get_bq_irq_state(void);

#endif
